define([
    'jquery',
    'underscore',
    'backbone',
    'module/article/models/ArticleModel',
    'module/article/models/ArticleCollection',
    'text!module/article/templates/listprice.html',
], function($, _, Backbone, /*jqueryui,*/ ArticleModel, ArticleCollection, listprice) {

    var ListPriceController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
            this.render();

        },
        events: {
            "click #edit_action": "edit_action"
        },
        render: function() {
            this.list_view();

        },
        list_view: function() {
            var self = this;
            $.ajax({
                url: 'server/api/article/find_all/1',
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var collection = new ArticleCollection(data.result);
                    var data = {
                        list: collection.models,
                        _: _
                    };
                    var compiledTemplate = _.template(listprice, data);
                    self.$el.html(compiledTemplate);
                }
            });
        },
        edit_action: function(event) {
            dialog_edit();
            td = event.target.parentNode.parentNode.children[3]
            var id = event.target.getAttribute('index');
            //td = event
            $("#article_id").val(id);
            $("#price_sale").val(td.innerHTML);
            $("#dialog-edit").dialog("open");
        }
    });
    return ListPriceController;
});
var td;
function dialog_edit() {
    $("#dialog-edit").dialog({
        width: 550,
        modal: true,
        autoOpen: false,
        buttons: {
            "Agregar": function() {
                td.innerHTML = $("#price_sale").val();
                $.post("server/api/article/editprice", {id: $("#article_id").val(),price_sale: $("#price_sale").val()},
                function(data) {
                    
                }, "json");
                
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });
}